package com.apes.autoparts.parts.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/***
 * 子组图片
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SubGroupImg.CONST_TABLE_NAME)
public class SubGroupImg extends BaseModel {

    public static final String CONST_TABLE_NAME = "auto_sub_group_img";


    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "子组图片")
    @Comment("唯一码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sub_group_id", referencedColumnName = "id")
    @Comment("子组")
    private SubGroup subGroupId;


    @Column(name = "org_url", length = 4000)
    @Comment("原始子组图片地址")
    private String orgUrl;


    @Column(name = "url", length = 4000)
    @Comment("服务器子组图片地址")
    private String url;


    @Column(name = "timestamp")
    @Comment("时间戳")
    private Date timestamp;


    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("有效标记")
    private boolean valid = true;

    @Column(name = "note", length = 1000)
    @Comment("备注")
    private String note; //备注

    /***
     * 图片
     */
    @OneToMany(mappedBy = "subGroupImg", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<SubGroupImgPoint> subGroupImgPoints = new ArrayList<>();

    public List<SubGroupImgPoint> getSubGroupImgPoints() {
        for (SubGroupImgPoint item : subGroupImgPoints) {
            item.setSubGroupImg(this);
        }
        return subGroupImgPoints;
    }


}
